package com.yiwei.jmx.view;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.text.Spanned;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.jmx.R;
import com.yiwei.jmx.util.Spans;
import com.yiwei.jmx.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/3/16 0016.
 */
public class YmdServiceDialog extends Dialog {


    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_agree)
    TextView txtAgree;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;
    private Context context;


    public YmdServiceDialog(Context context) {
        super(context, R.style.mydialog);
        this.context = context;
        init();
    }

    public YmdServiceDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public YmdServiceDialog setOnSelect(OnSureListener listener) {
        selectListener = listener;
        return this;
    }

    @Override
    public void show() {
        super.show();
    }

    @OnClick({R.id.btn_close, R.id.txt_agree, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                dismiss();
                break;
            case R.id.txt_agree:
                break;
            case R.id.btn_submit:
                if (selectListener != null) {
                    selectListener.onSure();
                    dismiss();
                }
                break;
        }
    }

    public YmdServiceDialog setData(String data) {
        int x32 = context.getResources().getDimensionPixelSize(R.dimen.x32);
        Spanned txt_one = Spans.with(context).font("授权 ", x32, 0xff666666).
                font(data, x32, 0xff000000).style(Typeface.BOLD).
                font(" 获取以下信息为你服务：", x32, 0xff666666).
                build();
        txtTitle.setText(txt_one);
        return this;
    }

    public interface OnSureListener {
        void onSure();
    }

    private OnSureListener selectListener;

    private void init() {
        Window window = this.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setWindowAnimations(R.style.mydialog_animation);
        setContentView(R.layout.dialog_ymd_service);
        ButterKnife.bind(this);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = (int) (Util.getDisplayMetrics(context).heightPixels * 0.85);
        } else {
            lp.width = (int) (Util.getDisplayMetrics(context).widthPixels * 0.85);
        }
        window.setAttributes(lp);
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        this.setCanceledOnTouchOutside(false);

        int x32 = context.getResources().getDimensionPixelSize(R.dimen.x32);
        Spanned txt_one = Spans.with(context).font("授权 ", x32, 0xff666666).
                font("有米贷", x32, 0xff000000).style(Typeface.BOLD).
                font(" 获取以下信息为你服务：", x32, 0xff666666).
                build();
        txtTitle.setText(txt_one);

        Spanned txt_two = Spans.with(context).font("同意 ", x32, 0xff666666).
                font("《用户授权协议》", x32, 0xff0072FF).
                build();
        txtAgree.setText(txt_two);
    }

}
